// pages/memberList/memberList.js
const app = getApp();
import {
  postRequest,
  getRequest,
  postParamsRequest
} from '../../api/request';
import moment from '../../utils/moment';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    themeColor: app.globalData.themeColor,
    subColor: app.globalData.subColor,
    btnColor: app.globalData.btnColor,
    myAccessWine: app.globalData.myAccessWine,
    starTime: '', //实时数据开始时间
    endTime: '', //实时数据开始时间
    keyword: '',
    orderList: [],
    orderPageNo: 1,
    orderPageSize: 10,
    orderLoadType: "",
    takeInfo: {
      isAll: 0, //是否全部取走，1全部，0部分
      deskName: '',
      id: '',
    },
    status: 2,
  },

  //切换状态时
  onStatusChange(e) {
    wx.pageScrollTo({
      scrollTop: 0,
      duration: 10
    })
    this.setData({
      status: e.currentTarget.dataset.status,
      orderPageNo: 1,
    })
    this.getFoodsOrderList(false);
  },

  //一键取走，部分取走
  getWine(e) {
    let item = e.currentTarget.dataset.item;
    this.setData({
      selectType: 1,
      'takeInfo.isAll': e.currentTarget.dataset.isAll,
      'takeInfo.id': item.id,
    })
    wx.navigateTo({
      url: '../accessWineGet/accessWineGet?isAll=' + this.data.takeInfo.isAll + '&id=' + this.data.takeInfo.id,
    })
    //this.showSelectModal();
  },
  //存单详细
  accessWineDetail(e) {
    let item = e.currentTarget.dataset.item;
    wx.navigateTo({
      url: '../accessWineDetail/accessWineDetail?id=' + item.id,
    })
  },
  //获取列表,isConcat 是否拼接
  getFoodsOrderList(isConcat) {
    let that = this;
    wx.showLoading({
      title: '加载中，请稍后……',
    })

    let params = {
      beid: app.beid,
      memberId: app.myInfo.memberId,
      pageNo: that.data.orderPageNo,
      pageSize: that.data.orderPageSize,
      keyword: that.data.keyword,
      startTime: '',
      endTime: '',
      status: that.data.status,
    }

    postRequest('/wmp/wineAccess/list', params).then((res) => {
      console.log(res.data.data);
      if (res.data.data != null) {
        //判断时间是否过期
        res.data.data.data.forEach((item,index)=>{
          let isTime = moment(moment().format('YYYY-MM-DD')).diff(moment(moment().format(item.expireTime)), 'days') > 0;
          if(isTime){
            item.isExpireTime =  true;
          }else{
            item.isExpireTime =  false;
          }
        });
        //合并数据
        let list = isConcat ? that.data.orderList.concat(res.data.data.data) : res.data.data.data;
        console.log(list);
        //判断是否还有数据
        let loadType = "";
        if (res.data.data.totalCount == 0) {
          loadType = "loadNull";
        } else {
          if (Math.ceil(res.data.data.totalCount / that.data.orderPageNo) <= that.data.orderPageSize) {
            loadType = "loadOver";
          } else {
            loadType = "loadMore";
          }
        }

        that.setData({
          orderList: list,
          orderLoadType: loadType,
          orderPageNo: that.data.orderPageNo + 1,
        })
      }
      wx.hideLoading();
    }).catch((err) => {
      wx.hideLoading();
    });
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    wx.pageScrollTo({
      scrollTop: 0,
      duration: 10
    })
    this.setData({
      orderPageNo: 1,
    })
    //如果没有token,则需要重新登录获取个人信息等，避免缺少id导致数据无法获取
    if (app.token == "" || app.token == undefined) {
      app.initLogin().then(res => {
        this.getFoodsOrderList(false);
      }).catch(err => {})
    } else {
      this.getFoodsOrderList(false);
    }
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    if (this.data.orderLoadType == "loadMore") {
      this.setData({
        orderLoadType: "",
      });
      this.getFoodsOrderList(true);
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})